/*
 * @Author: your name
 * @Date: 2024-03-29 10:16:46
 * @LastEditTime: 2024-03-29 10:21:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\206.反转链表.cpp
 */
/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* tmp = nullptr;
        ListNode* pre = nullptr;
        ListNode* cur = head;
        while(cur){
            tmp = cur->next;
            cur->next = pre;
            // 更新
            pre = cur;
            cur = tmp;
            
        }
        return pre;
    }
};
// @lc code=end
// 不需要虚拟节点就可以反转
